<?php

class FriendControl extends Control {

    public function friends()
	{
        $uid = self::isauth();
        if (!$uid) return $this->out_error(ERR303);
        
        $ret = modcall("friend", "friends", array($uid));
        if ($ret['ecode']) return $this->out_error($ret['ecode']);
        else return $this->out_result(&$ret['ret']);
    }

    public function richfriends()
	{
        $uid = self::isauth();
        if (!$uid) return $this->out_error(ERR303);
        
        $page = intval($_POST['page']);
        $pagesize = intval($_POST['pagesize']);
        $page = $page ? $page : 1;
        $pagesize = $pagesize ? $pagesize : 500;
        $ret = modcall("friend", "richfriends", array($uid, $page, $pagesize));
        if($ret['ecode']) return $this->out_error($ret['ecode']);
		else $this->out_result(&$ret['ret']);
    }

	public function osfriends()
	{
		if(!snscall("opensocial", "check_signature", array("yaba"))) return $this->out_error(ERR302);

		$sid = $_POST['sid'];
		$uid = modcall("user", "auth", array($sid));
		$fids = $_POST['fids'];
		if(!$uid || !$fids) return $this->out_error(ERR302);

		$fids = explode(",", $fids);
		if(!$fids) return $this->out_error(ERR302);
		$res = array();
		foreach($fids as $oid)
		{
			if(!$oid) continue;
			$oidsppos = stripos($oid, ":");
			if(!$oidsppos) continue;
			$oid = substr($oid, $oidsppos + 1);
			$oid = intval($oid);
			if($oid < 1) continue;

			$res[$oid] = $oid;
		}

		$ret = modcall("friend", "osfriends", array($uid, $res));
        if($ret['ecode']) return $this->out_error($ret['ecode']);
		else $this->out_result(&$ret['ret']);
	}

    /**
     * 进行好友第一次访问奖励
     * @return type 
     */
    public function getisnew()
	{
        $uid = self::isauth();
        if (!$uid) return $this->out_error(ERR303);
        
        $fuid = intval($_POST['fuid']);
        if (!$fuid) return $this->out_error(ERR302);
        
        $ret = modcall("friend", "getisnew", array($uid, $fuid));
        if($ret['ecode']) return $this->out_error($ret['ecode']);
        else $this->out_result(&$ret['ret']);
    }

}